package com.cloud.retry.controller;

import com.alibaba.fastjson.JSON;
import com.cloud.retry.annotation.LogApi;
import com.cloud.retry.aop.LogAspect;
import com.cloud.retry.entity.LogEntity;
import com.cloud.retry.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;

@RestController
public class RetryController {
    @Autowired
    private LogService logService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/retry")
    public void retry(@RequestParam Long id) throws Exception {
        LogEntity entity = logService.getById(id);
        try {
            HandlerMethod handlerMethod = LogAspect.map.get(entity.getType());
            Object object = applicationContext.getBean(handlerMethod.getBeanType());
            Object body = JSON.parseObject(entity.getRequestBody(), handlerMethod.getMethodParameters()[0].getParameterType());
            handlerMethod.getMethod().invoke(object, body);
        } catch (Exception e) {
            throw e;
        }
    }

    @GetMapping("/retryTest")
    @LogApi("retryTest")
    public void retryTest(@RequestParam("name") String name) throws Exception {
        System.out.println(name);
    }
}